package com.oddfar.campus.business.service.Impl;

import com.oddfar.campus.business.domain.Tcwxx;
import com.oddfar.campus.business.mapper.TcwxxMapper;
import com.oddfar.campus.common.domain.PageResult;
import com.oddfar.campus.common.exception.ServiceException;
import com.oddfar.campus.common.utils.SecurityUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oddfar.campus.business.domain.ClTcw;
import com.oddfar.campus.business.mapper.ClTcwMapper;
import com.oddfar.campus.business.service.ClTcwService;
@Service
public class ClTcwServiceImpl extends ServiceImpl<ClTcwMapper, ClTcw> implements ClTcwService{

    @Autowired
    private ClTcwMapper clTcwMapper;

    @Autowired
    private TcwxxMapper tcwxxMapper;

    @Override
    public PageResult<ClTcw> page(ClTcw clTcw) {
        Long userId = SecurityUtils.getUserId();
        Long phonenumber = clTcwMapper.selectdjxhByuserid(userId);
        if (userId != 1) {
            return clTcwMapper.selectPage(clTcw, phonenumber);
        }
        return clTcwMapper.selectPage(clTcw);
    }

    @Override
    public int insertClTcw(ClTcw clTcw) {
        clTcw.setUuid(java.util.UUID.randomUUID().toString());
        String clpzh = clTcw.getClpzh();
        clpzh = clpzh.replaceAll("[\\t\\n\\r]", "");
        clpzh = clpzh.trim();
        clTcw.setClpzh(clpzh);
        clTcw.setDjxh(clTcwMapper.selectDjxhByClpzh(clpzh));
        //判断登记序号是否为空
        if (clTcw.getDjxh() == null || clTcw.getDjxh().toString().length() < 1){
            throw new ServiceException("新增预约失败，未找到"+clTcw.getPhonenumber()+":"+clTcw.getClpzh()+"对应的车辆信息");
        }
        clTcw.setTcw(clTcwMapper.selectTcwByTcwid(clTcw.getTcwid()));
        //设置当前可用标志为0，结束后将可用标志改回1的操作由xxl-job执行
        tcwxxMapper.setDqkybzEqZero(clTcw.getTcwid());
        return clTcwMapper.insert(clTcw);
    }

    @Override
    public int updateClTcw(ClTcw clTcw) {
        return 0;
    }

    // 更新当前可用标志
    @XxlJob("updateDqkybzEqOneJobHandler")
    public void updateDqkybzEqOne(){
        tcwxxMapper.updateDqkybzEqOne();
    }

}
